from flask import *
from flask_sqlalchemy import *
import os


class Config:
    DEBUG = True
    APP_PATH = os.path.dirname(__file__)
    SQLALCHEMY_DATABASE_URI = 'mysql://root:123@127.0.0.1:3306/study'
    SQLALCHEMY_TRACK_MODIFICATIONS = True


app = Flask(__name__)
app.config.from_object(Config)  # 先指定配置类
# 创建数据库链接
db = SQLAlchemy(app)


class Husband(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(10))
    # 只是表示了一种关系, 但是不会在数据库中创建字段
    wife = db.relationship("Wife", backref="husband", uselist=False)


class Wife(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(10))
    husband_id = db.Column(db.Integer, db.ForeignKey("husband.id"))


if __name__ == '__main__':
    db.create_all()
    h = Husband()
    h.name = "zyd"
    db.session.add(h)
    db.session.commit()

    w = Wife()
    w.name = "彭十六"
    w.husband_id = h.id
    db.session.add(w)
    db.session.commit()
